﻿Imports Negocio
Imports DataLayer
Imports Excepciones

Public Class GesRecursos

    Public Shared Function Obtener(dni As String) As Recurso
        Return Obtener(0, dni)
    End Function

    Public Shared Function Obtener(OID As Integer) As Recurso
        Return Obtener(OID, Nothing)
    End Function

    Private Shared Function Obtener(OID As Integer, dni As String) As Recurso
        Dim r As New Recurso()
        Try
            Dim repository As IRepository = RepositoryFactory.Create()
            repository.Nombre = "RECURSOS_PROC"
            repository.AddParameter("@ACCION", "4")
            repository.AddParameter("@ID", OID)
            repository.AddParameter("@DNI", dni)
            Dim dtRecurso As DataTable = repository.ExecuteDataTable()

            For Each dr As DataRow In dtRecurso.Rows
                r.Persistencia = EstadoPersistencia.UPDATE
                r.OID = Convert.ToInt32(dr.Item("recID"))
                r.Nombre = dr.Item("recNOMBRE").ToString()
                r.Apellido = dr.Item("recAPELLIDO").ToString()
                r.Dni = dr.Item("recDNI").ToString()
                r.Email = dr.Item("recEMAIL").ToString()
                r.Tipo = Convert.ToInt32(dr.Item("recTIPO"))
            Next

        Catch ex As Exception
            Throw New ResourceGetException()
        End Try

        Return r
    End Function

    Public Shared Sub Guardar(recurso As Recurso)
        Try

            Dim repository As IRepository = RepositoryFactory.CreateTransactional()
            repository.Nombre = "RECURSOS_PROC"
            repository.AddParameter("@ACCION", recurso.Persistencia)
            repository.AddParameter("@ID", recurso.OID)
            repository.AddParameter("@DNI", recurso.Dni)
            repository.AddParameter("@NOMBRE", recurso.Nombre)
            repository.AddParameter("@APELLIDO", recurso.Apellido)
            repository.AddParameter("@EMAIL", recurso.Email)
            repository.AddParameter("@TIPO", recurso.Tipo)
            repository.ExecuteNonQuery()
            repository.Commit()
        Catch ex As Exception
            Throw New ResourceSaveException()
        End Try
        GesBitacoras.Guardar(recurso.Persistencia, "ModRecurso", New String() {"Recurso", recurso.Dni})
    End Sub

    Public Shared Sub Eliminar(recurso As Recurso)
        VerificarDependencia(recurso)
        recurso.Persistencia = EstadoPersistencia.DELETE
        Guardar(recurso)
    End Sub


    Private Shared Sub VerificarDependencia(recurso As Recurso)

        Dim msg As String = ""
        Try

            Dim repository As IRepository = RepositoryFactory.Create()
            repository.Nombre = "RECURSOS_PROC"
            repository.AddParameter("@ACCION", "RELACION")
            repository.AddParameter("@ID", recurso.OID)
            repository.AddParameter("@DNI", Nothing)
            repository.AddParameter("@NOMBRE", Nothing)
            repository.AddParameter("@APELLIDO", Nothing)
            msg = Convert.ToString(repository.ExecuteScalar())

        Catch ex As Exception
            Throw New ResourceGetException()
        End Try

        If msg <> "" Then
            Throw New CustomException(msg, "")
        End If

    End Sub

    Public Shared Function ValidarDni(recurso As Recurso) As Boolean
        Return True
    End Function

    Public Shared Function Consultar(dni As String, nombre As String, apellido As String) As List(Of Recurso)

        If String.IsNullOrWhiteSpace(dni) Then
            dni = Nothing
        End If
        If String.IsNullOrWhiteSpace(nombre) Then
            nombre = Nothing
        End If
        If String.IsNullOrWhiteSpace(apellido) Then
            apellido = Nothing
        End If
        Dim recursos As New List(Of Recurso)
        Try
            Dim repository As IRepository = RepositoryFactory.Create()
            repository.Nombre = "RECURSOS_PROC"
            repository.AddParameter("@ACCION", "4")
            repository.AddParameter("@ID", Nothing)
            repository.AddParameter("@DNI", dni)
            repository.AddParameter("@NOMBRE", nombre)
            repository.AddParameter("@APELLIDO", apellido)
            Dim dtRecurso As DataTable = repository.ExecuteDataTable()

            For Each dr As DataRow In dtRecurso.Rows
                Dim r As New Recurso()
                r.Persistencia = EstadoPersistencia.UPDATE
                r.OID = Convert.ToInt32(dr.Item("recID"))
                r.Nombre = dr.Item("recNOMBRE").ToString()
                r.Apellido = dr.Item("recAPELLIDO").ToString()
                r.Dni = dr.Item("recDNI").ToString()
                r.Email = dr.Item("recEMAIL").ToString()
                r.Tipo = Convert.ToInt32(dr.Item("recTIPO"))
                recursos.Add(r)
            Next

        Catch ex As Exception
            Throw New ResourceGetException()
        End Try

        Return recursos

    End Function

End Class
